package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.*;
import one.stand.request.GoodSpecRequest;
import one.stand.request.SecKillRequest;
import one.stand.response.SecKillResponse;
import one.stand.service.BaseService;
import one.stand.service.SecKillService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Hongcj
 * @CreateDate: 2019/7/19 11:38
 * @Description:
 */
@Service
@Transactional
public class SecKillServiceImpl extends BaseService implements SecKillService {

    @Override
    public ResultModel<List<SecKillResponse>> selectKillGoodsList(SecKillRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (request.getDraftType() == null) {
            return ResultModel.checkParamFail("draftType草稿类型为空");
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<SecKillModel> list = secKillMapper.selectKillGoodsList(new SecKillModel() {{
            setDraftType(request.getDraftType());
            setCompanyId(request.getCId().intValue());
        }});
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        PageInfo<SecKillModel> pageInfo = new PageInfo<>(list);
        List<SecKillResponse> responseList = new ArrayList<>();
        pageInfo.getList().forEach(obj -> {
            {
                SecKillResponse response = new SecKillResponse();
                response.setTotalBuyNum(secKillMapper.selectTotalBuyNum(obj));
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<Boolean> addKillGoodsInfo(SecKillRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        if (request.getStartTime() == null || request.getEndTime() == null) {
            return ResultModel.fail("请输入开始时间和结束时间~！");
        }
        if (request.getUpdateType() == null) {
            return ResultModel.fail("请传入UpdateType~！");
        }

        // 草稿箱修改时(UpdateType为1) 不校验以下参数
        if (request.getUpdateType() == 0) {
            if (request.getKillType() == null || request.getMaxPurchase() == null) {
                return ResultModel.fail("请输入完整秒杀参数配置~！");
            }
            if (request.getGoodSpecRequestList() == null) {
                return ResultModel.fail("请输入秒杀金额和平台库存~！");
            }
        }


        GoodsModel goodsModel = goodsMapper.selectByPrimaryKey(request.getGoodsId());
        // 如果该商品是拼团则抛出异常
        if(goodsModel.getIsAssemble() == 1){
            return ResultModel.fail("该商品已经有拼团配置~！");
        }

        SecKillModel model = new SecKillModel();
        BeanUtils.copyProperties(request, model);
        // 如果有秒杀状态 则将该条数据设置为草稿箱状态
        if (goodsModel.getIsKill() == 1) {
            model.setDraftType(0);
            model.setState(1);
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            model.setStartTime(formatter.parse(request.getStartTime()));
            model.setEndTime(formatter.parse(request.getEndTime()));
        } catch (ParseException e) {
            log.info("添加秒杀商品配置[异常]", e);
        }

        SecKillModel secKillModel = secKillMapper.selectKillGoodsInfo(new SecKillModel() {{
            setIsDel(0);
            setState(1); // 秒杀进行中
            setGoodsId(request.getGoodsId());
        }});

        GoodsAssembleModel goodsAssemble = goodsAssembleMapper.getGoodsAssemble(request.getGoodsId());
        if (goodsAssemble != null) {
            return ResultModel.fail("该商品已经有拼团配置~！");
        }

        // 保存商品平台配置
        if (secKillModel != null) {
            secKillMapper.updateByPrimaryKeySelective(model);
        } else {
            model.setKillNo(getKillNo());
            model.setIsDel(0);
            model.setCreateTime(currentDate());
        }


        // 修改商品规格秒杀配置
        if (null != request.getGoodSpecRequestList()) {
            for (GoodSpecRequest request1 : request.getGoodSpecRequestList()) {
                GoodSpecModel model1 = new GoodSpecModel();
                model1.setSpecId(request1.getSpecId());
                model1.setKillNum(request1.getKillNum());
                model1.setKillPrice(request1.getKillPrice());
                goodSpecMapper.updateByPrimaryKeySelective(model1);
            }
        }

        if (request.getDraftType() == 1) {
            // 修改商品秒杀状态
            goodsMapper.updateByPrimaryKeySelective(new GoodsModel() {{
                setGoodsId(request.getGoodsId());
                setIsKill(1);
                setState(1);
                setAssembleTime(new Date());
            }});

            model.setStart(1); // 秒杀活动进行中
        }
        secKillMapper.addKillGoodsInfo(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Boolean> delKillGoodsInfo(SecKillRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (request.getKillId() == null) {
            return ResultModel.checkParamFail("killId秒杀为空");
        }
        SecKillModel model = new SecKillModel();
        BeanUtils.copyProperties(request, model);
        model.setIsDel(1);
        secKillMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<SecKillResponse> updateKillGoodsInfo(SecKillRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (request.getKillId() == null) {
            return ResultModel.checkParamFail("killId秒杀为空");
        }
        SecKillModel model = new SecKillModel();
        BeanUtils.copyProperties(request, model);
        List<SecKillModel> list = secKillMapper.updateKillGoodsInfo(model);

        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }

        SecKillResponse response = new SecKillResponse();
        BeanUtils.copyProperties(list.get(0), response);

        response.setTotalBuyNum(secKillMapper.selectTotalBuyNum(model));

        response.setGoodSpecModelList(goodSpecMapper.getSpec(new GoodSpecModel() {{
            setGoodsId(response.getGoodsId());
        }}));
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<Boolean> releaseKillGoods(SecKillRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (request.getKillId() == null) {
            return ResultModel.checkParamFail("killId秒杀为空");
        }

        // 查询商品是否有拼团状态
        GoodsModel goodsModel = goodsMapper.selectByPrimaryKey(request.getGoodsId());
        boolean isAssemble = (goodsModel.getIsAssemble() == 1);
        if (isAssemble) {
            return ResultModel.fail("该商品已有拼团状态");
        } else {
            SecKillModel model = new SecKillModel();
            BeanUtils.copyProperties(request, model);
            model.setDraftType(1);
            model.setState(1);
            secKillMapper.updateByPrimaryKeySelective(model);

            // 修改商品秒杀状态
            goodsMapper.updateByPrimaryKeySelective(new GoodsModel() {{
                setGoodsId(request.getGoodsId());
                setIsKill(1);
                setAssembleTime(new Date());
            }});
        }
        return ResultModel.success(true);
    }
}
